﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Automation;
using System.Diagnostics.Contracts;

namespace System.Windows.Automation
{
    /// <summary>
    /// Extends <see cref="AutomationElement"/>.
    /// </summary>
    public static class AutomationElementExtensions
    {
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TPattern"></typeparam>
        /// <param name="element"></param>
        /// <param name="pattern"></param>
        /// <returns></returns>
        public static TPattern ToPattern<TPattern>(this AutomationElement element, AutomationPattern pattern)
            where TPattern : BasePattern
        {
            Contract.Requires<ArgumentNullException>(element != null, "element");

            var pat = (TPattern)element.GetCurrentPattern(pattern);
            if (pat == null)
                throw new InvalidCastException("AutomationElement does not implement the requested pattern!");
            return pat;
        }

    }
}
